package cn.zwy.asm;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import cn.zwy.bean.UserBean;

public class Application {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ClassPrinter classPrinter = new ClassPrinter();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("cn.zwy.asm.P".replace(".", "/")+".class");
        ClassReader cr = new ClassReader(is);
        cr.accept(classPrinter, 0);
    }

    // copy
    public static void copy() throws IOException {
        MyClassLoader loader = new MyClassLoader("loader1");
        loader.setPath("D:\\myapp\\otherlib\\");
        byte[] b1 = loader.loadClassData("DogV7");
        ClassWriter cw = new ClassWriter(0);
        // cv 将所有事件转发给 cw
        ClassVisitor cv = new AddMethodAdapter(cw);
        ClassReader cr = new ClassReader(b1);
        cr.accept(cv, 0);
        byte[] b2 = cw.toByteArray(); // b2 与 b1 表示同一个类
        FileOutputStream fos = new FileOutputStream(new File("D:\\myapp\\otherlib\\", "DogV7.class"));
        fos.write(b2, 0, b2.length);
        fos.flush();
        fos.close();
    }

    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {

            // 取出每一个字符
            char c = string.charAt(i);

            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }

    public static String toChineseHex(String s)

    {

        String ss = s;

        byte[] bt = ss.getBytes();

        String s1 = "";

        for (int i = 0; i < bt.length; i++)

        {

            String tempStr = Integer.toHexString(bt[i]);

            if (tempStr.length() > 2)

                tempStr = tempStr.substring(tempStr.length() - 2);

            s1 = s1 + tempStr + " ";

        }

        return s1.toUpperCase();

    }
}
